/**
 * 参数设置管理初始化
 */
var CurrencyList = {
    id: "CurrencyListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

CurrencyList.formatDate = function (shijianchuo) {
	var add0 = function add0(m){return m<10?'0'+m:m }

	if(shijianchuo=="" || shijianchuo == undefined)return null;
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}

CurrencyList.formatDate_8 = function (shijianchuo) {
	var add0 = function add0(m){return m<10?'0'+m:m }

	if(shijianchuo=="" || shijianchuo == undefined)return null;
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours()-8;
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}

/**
 * 初始化表格的列
 */
CurrencyList.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '记录号', field: 'recharge_no', visible: false, align: 'center', valign: 'middle',width:'50px'},
        {title: '状态', field: 'status', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {//自定义显示，这三个参数分别是：value该行的属性，row该行记录，index该行下标
        		var data;
        		switch(value)
        		{
        		case 0: data="订单生成"; break;
        		case 1: data="待转账"; break;
        		case 2: data="已充值，待审核"; break;
        		case 3: data="转账失败"; break;
        		case 4: data="已完成"; break;
        		case 5: data="已取消"; break;
        		default: data =row.status;break;
        		}
                return data;
            }

        },
        {title: '申请时间', field: 'add_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {

                console.log("申请时间："+row.add_time);

        		return CurrencyList.formatDate(row.add_time);
        	}
        },
        {title: '姓名', field: 'full_name', align: 'center', valign: 'middle'},
        {title: '绑定手机', field: 'binding_phone', align: 'center', valign: 'middle'},
        {title: '通证名称', field: 'currency', align: 'center', valign: 'middle'},
        {title: '提币数量', field: 'amount', align: 'center', valign: 'middle'},
        {title: '手续费（通证）', field: 'poundage', align: 'center', valign: 'middle'},
        {title: '提币地址', field: 'remarkInfo', align: 'center', valign: 'middle'},
        
        {title: '转账矿工费（ETH）', field: 'miner', align: 'center', valign: 'middle'},
        {title: '转账交易时间', field: 'confirm_transfer_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {
    		return CurrencyList.formatDate(row.confirm_transfer_time);
    	}
        },
        {title: '跟单人账号', field: 'doc_account', align: 'center', valign: 'middle'},
        {title: '跟单时间', field: 'doc_time', align: 'center', sortable: true,valign: 'middle',
        	formatter: function (value, row, index) {
        		return CurrencyList.formatDate(row.doc_time);
        	}
        },
        {title: '审核时间', field: 'audit_time', align: 'center', valign: 'middle',
        	formatter: function (value, row, index) {
        		return CurrencyList.formatDate(row.audit_time);
        	}
        },
    ]
    return columns;
};

/**
 * 检查是否选中
 */
CurrencyList.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CurrencyList.seItem = selected[0];
        return true;

    }
};

/**
 * 检查是否可以停用
 */
CurrencyList.checkstopuse = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    CurrencyList.seItem = selected[0];
    if(CurrencyList.seItem.zhuangtai == "停用"){
        Feng.info("这个合伙人状态已经是停用");
        return false;
    }else{
        CurrencyList.seItem = selected[0];
        return true;

    }
};

/**
 * 检查是否可以启用
 */
CurrencyList.checkuse = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    CurrencyList.seItem = selected[0];
    if(CurrencyList.seItem.zhuangtai == "启用"){
        Feng.info("这个合伙人状态已经是启用");
        return false;
    }else{
        return true;
    }
};

/**
 * 停用确认框
 */
CurrencyList.CurrencyStopUse = function () {
    if(this.check()) {
        if(this.checkstopuse()) {
        $.confirm({
            title: '系统提示',
            content: '是否确定停用此合伙人,登录账号:' + CurrencyList.seItem.binding_phone,
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: CurrencyList.stopUse()
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                }
            }
        });
    }
    }
};

/**
 * 启用确认框
 */
CurrencyList.CurrencyUse = function () {
    if(this.check()) {
        if(this.checkuse()) {
        $.confirm({
            title: '系统提示',
            content: '是否确定启用此合伙人,登录账号:' + CurrencyList.seItem.binding_phone,
            buttons: {
                ok: {
                    text: '确认',
                    btnClass: 'btn-primary',
                    action: CurrencyList.Use()
                },
                cancel: {
                    text: '取消',
                    btnClass: 'btn-primary',
                    action: function () {
                        // button action.
                    }
                },
            }
        });
     }
    }
};

/**
 * 打开查看参数设置详情
 */
CurrencyList.CurrencyUpdate = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/currencyList/currency_update/' + CurrencyList.seItem.recharge_no
        });
        this.layerIndex = index;
    }
};

/**
 * 查看合同详情
 */
CurrencyList.CurrencyContractDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '合同详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/currencyList/contractDetail/' + CurrencyList.seItem.recharge_no
        });
        this.layerIndex = index;
    }
};

/**
 * 跟单
 */
CurrencyList.toDoc = function () {
	if (this.check()) {
		
		var ajax = new $ax(Feng.ctxPath + "/currencyList/doc", function (data) {
	    	if(data["code"]==2){
	    		Feng.error(data["message"] + "!");
	    	}else{
	        Feng.success("跟单成功!");
	        window.parent.CurrencyList.table.refresh();
	    	}
		}, function (data) {
			Feng.error("失败!" + data.responseJSON.message + "!");
		});
		ajax.set("recharge_no", this.seItem.recharge_no);
		ajax.start();
		
	}
};
/**
 * 停用
 */
CurrencyList.StopUse = function () {
    if (this.check()) {

            var ajax = new $ax(Feng.ctxPath + "/currencyList/stopUse", function (data) {
                Feng.success("停用成功!");
                CurrencyList.table.refresh();
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("recharge_no", this.seItem.recharge_no);
            ajax.start();

    }
};

/**
 * 启用
 */
CurrencyList.Use = function () {
    if (this.check()) {

            var ajax = new $ax(Feng.ctxPath + "/currencyList/use", function (data) {
                Feng.success("启用成功!");
                CurrencyList.table.refresh();
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("recharge_no", this.seItem.recharge_no);
            ajax.start();

    }
};

/**
 * 查询参数设置列表
 */
CurrencyList.search = function () {
    var queryData = {};
    queryData['binding_phone'] = $("#binding_phone").val();
    queryData['status'] = $("#status").val();
    queryData['disUsername'] = $("#disUsername").val();
    queryData['add_time'] = CurrencyList.formatDate_8($("#add_time").val());//$("#add_time").val().replace("/","-");
    if($("#add_time").val() != "") {
        if($("#add_time_end").val() == "") {
            Feng.info("请选择结束时间！");
            return false;
        }else{
            queryData['add_time_end'] = CurrencyList.formatDate_8($("#add_time_end").val());//$("#add_time_end").val().replace("/","-");
        }
    }
    CurrencyList.table.refresh({query: queryData});
};

$(function () {
    $("#add_time").attr('type','date');
    $("#add_time_end").attr('type','date');
    var defaultColunms = CurrencyList.initColumn();
    var table = new BSTable(CurrencyList.id, "/currencyList/list", defaultColunms);
    table.setPaginationType("server");
    CurrencyList.table = table.init();
});



